from pydantic import Extra

from apitest.engine.client.http import HttpClient
from apitest.engine.const.enums import RType
from apitest.engine.const.model import Auth, EnvModel
from apitest.engine.parse import parse_request, parse_response
from apitest.lib.fwe_lib import model
from apitest.lib.fwe_lib.route import MotorFweApiRoute

PSM = "motor.fwe.api"


class FWApiService(HttpClient):

    def __init__(self, app: str, auth: Auth = None, env: EnvModel = None, rtype: RType = RType.obj, **kwargs):
        super(FWApiService, self).__init__(app=app, psm=PSM, auth=auth, env=env, **kwargs)
        self.rtype = rtype

    @parse_request
    def GetUserFWAccount(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.get(MotorFweApiRoute.MotorFweUserAccount, **kwargs)
        model.GetUserFWAccountRsp.Config.extra = extra
        resp = parse_response(resp, model.GetUserFWAccountRsp)
        return resp[rtype]

    @parse_request
    def FWAccountLogin(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.post(MotorFweApiRoute.MotorFweAccountLogin, **kwargs)
        model.OperateRsp.Config.extra = extra
        resp = parse_response(resp, model.OperateRsp)
        return resp[rtype]

    @parse_request
    def FWLogin(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.post(MotorFweApiRoute.MotorFweLogin, **kwargs)
        model.OperateRsp.Config.extra = extra
        resp = parse_response(resp, model.OperateRsp)
        return resp[rtype]

    @parse_request
    def FWIsLogin(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.get(MotorFweApiRoute.MotorFweIsLogin, **kwargs)
        model.OperateRsp.Config.extra = extra
        resp = parse_response(resp, model.OperateRsp)
        return resp[rtype]

    @parse_request
    def FWAuthBind(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.post(MotorFweApiRoute.MotorFweAuthBind, **kwargs)
        model.OperateRsp.Config.extra = extra
        resp = parse_response(resp, model.OperateRsp)
        return resp[rtype]

    @parse_request
    def GetBindInfo(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.get(MotorFweApiRoute.MotorFweBindInfo, **kwargs)
        model.GetBindInfoRsp.Config.extra = extra
        resp = parse_response(resp, model.GetBindInfoRsp)
        return resp[rtype]

    @parse_request
    def GetBindTicket(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.get(MotorFweApiRoute.MotorFweBindTicket, **kwargs)
        model.GetBindTicketRsp.Config.extra = extra
        resp = parse_response(resp, model.GetBindTicketRsp)
        return resp[rtype]

    @parse_request
    def GetExternalBusiness(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.get(MotorFweApiRoute.MotorFweExternalBusiness, **kwargs)
        model.GetExternalBusinessRsp.Config.extra = extra
        resp = parse_response(resp, model.GetExternalBusinessRsp)
        return resp[rtype]

    @parse_request
    def GetLogOffStatus(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.get(MotorFweApiRoute.MotorFweLogOffStatus, **kwargs)
        model.GetLogOffStatusResp.Config.extra = extra
        resp = parse_response(resp, model.GetLogOffStatusResp)
        return resp[rtype]

    @parse_request
    def ApplyLogOff(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.post(MotorFweApiRoute.MotorFweLogOffApply, **kwargs)
        model.OperateRsp.Config.extra = extra
        resp = parse_response(resp, model.OperateRsp)
        return resp[rtype]

    @parse_request
    def SendOperateVerifyCode(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.post(MotorFweApiRoute.MotorFweOperateVerifyCodeSend, **kwargs)
        model.OperateRsp.Config.extra = extra
        resp = parse_response(resp, model.OperateRsp)
        return resp[rtype]

    @parse_request
    def FWLoginWithSign(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.post(MotorFweApiRoute.MotorFweLoginWithSign, **kwargs)
        model.OperateRsp.Config.extra = extra
        resp = parse_response(resp, model.OperateRsp)
        return resp[rtype]

    @parse_request
    def FWAppUpdateInfo(self, extra: Extra = Extra.ignore, rtype: RType = None, **kwargs):
        rtype = rtype or self.rtype
        resp = self.client.get(MotorFweApiRoute.MotorFweAppUpdateInfo, **kwargs)
        model.GetAppUpdateDataResp.Config.extra = extra
        resp = parse_response(resp, model.GetAppUpdateDataResp)
        return resp[rtype]
